/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#include "bsp.h"
#include "bsp_api.h" 
#include "os_obj.h"
#include "mbapp.h"
#include "com_def.h"
#include "cpu_it.h"

#define mb_port                     UART4
#define mb_isr                      MB_UART4_IRQHandler
#define mbURT_IRQn                  UART4_IRQn
#define RCU_USARTRST                UART4_RST
#define mbObj                       mb.obj04
#define mbCmdBuff					mb.CmdBuff04

#define MB_RX_FIFO_LEN				14
#define MB_TX_FIFO_LEN				14

//RS485 Dir-Pin
#define mb_rx_dir_enable()  		PB13 = 1
#define mb_tx_dir_enable()  		PB13 = 0

#include "bsp_mbport_common.h"
#include "mb_fun.h"

/*****************************************************************************//*!
* @brief   mb port.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void mb_port_hal_cfg(void)
{
    intx_alloc();
    
    intx_disable();


    intx_enable();
}

/*****************************************************************************//*!
* @brief   mb_cb_set
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void mb_cb_set(void)
{
	intx_alloc();
	
    mbObj.slave.reg_coils_cb = &smb_reg_coils_cb;
    mbObj.slave.reg_discrete_cb = &smb_reg_discrete_cb;
    mbObj.slave.reg_hold_cb = &smb_reg_holding_cb;
    mbObj.slave.reg_input_cb = &smb_reg_input_cb;

    mmb_cmd_buff_init(&mbObj, mbCmdBuff, 32, 16);

    intx_disable();
	
    mbObj.api = &mb_fun;
	
    intx_enable();
	
	app_timer_task_add(_rtu_timer_run);
}

/*****************************************************************************//*!
* @brief   mb init .
* @param   mbMode
* @param   ulBaudRate
* @param   eParity
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void mb04_Init(mb_mode_type mbMode, uint32_t ulBaudRate, mb_parity_type eParity)
{
	// 禁用中断
	nvic_irq_set(mbURT_IRQn,0x0F,0);
	
	// 中断映射
	cpu_it.uart4 = mb_isr;
	
	// 初始化
   mb_port_init(mbMode, ulBaudRate, eParity);
}

